#include "LoaderController.h"

LoaderController::LoaderController(int leftPin, int rightPin, int loaderPin) {
    // this->leftServo = Servo();
    // this->leftServo.attach(leftPin);
    // this->rightServo = Servo();
    // this->rightServo.attach(rightPin);
    // this->loaderServo = Servo();
    // this->loaderServo.attach(loaderPin);
    // this->wifiServer = new WiFiServer();
}

LoaderController::~LoaderController() {

}

bool LoaderController::startWiFiWithAPMode() {
  WiFi.mode(WIFI_AP);
  bool result = WiFi.softAP(AP_NameChar, AP_PasswordChar);
  this->startTcpSocketService();
  return result;
}

void LoaderController::startTcpSocketService() {
    // this->wifiServer.begin(8266);
}

void LoaderController::socketLoop() {

    // uint8_t i;
    // //检测服务器端是否有活动的客户端连接
    // if (this->wifiServer.hasClient()) {       
    //     for (i = 0; i < MAX_SRV_CLIENTS; i++)
    //     //查找空闲或者断开连接的客户端，并置为可用
    //     {
    //         if (!this->serverClients[i] || !this->serverClients[i].connected())
    //         {
    //             if (this->serverClients[i]) this->serverClients[i].stop();  //未连接,就释放
    //             this->serverClients[i] = this->wifiServer.available();          //分配新的
    //           //  Serial.print("New client: "); Serial.println(i);
    //             continue;
    //         } 
    //     }
    //      //若没有可用客户端，则停止连接
    //     WiFiClient serverClient = this->wifiServer.available();
    //     serverClient.stop();
    // }
    // //检查客户端的数据
    // for (i = 0; i < MAX_SRV_CLIENTS; i++)
    // {
    //     if (this->serverClients[i] && this->serverClients[i].connected())
    //     {   
    //         if (this->serverClients[i].available())
    //         {
    //             Serial.println("Connected to client !!! ");
    //              //从Telnet客户端获取数据
    //             while (this->serverClients[i].available()) {
    //                 data[ind] = this->serverClients[i].read(); //读取client端发送的字符
    //                 ind++;
    //             }
    //             for(int j=0;j < ind; j++)
    //             {
    //                 Serial.print(data[j]);
    //             }
    //             Serial.println();
    //             ind = 0;
    //             this->serverClients[i].print("OK! Got your request.");        //在client端回复       
    //         }     
    //     }
    // }  
}

